package com.joolun.web.controller.customers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.joolun.system.domain.ScrmDeliveryPlan;
import com.joolun.weixin.service.IScrmCustomerInfoService;
import com.joolun.weixin.service.IScrmDeliveryPlanService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.system.domain.ScrmCustomerInfo;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 企业微信客户详情信息Controller
 * 
 * @author joolun
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/customers/info")
public class ScrmCustomerInfoController extends BaseController
{
    @Autowired
    private IScrmCustomerInfoService scrmCustomerInfoService;

    @Autowired
    private IScrmDeliveryPlanService scrmDeliveryPlanService;

    /**
     * 查询企业微信客户详情信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ScrmCustomerInfo scrmCustomerInfo) {
        startPage();
        List<ScrmCustomerInfo> list = scrmCustomerInfoService.selectScrmCustomerInfoList(scrmCustomerInfo);
        // 投放计划Ids
        List<Long> planIds = list.stream().map(item -> item.getPlanId()).filter(Objects::nonNull).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(planIds)){
            List<ScrmDeliveryPlan> scrmDeliveryPlans =  scrmDeliveryPlanService.listByIds(planIds);
            Map<Long,String> planNameMap =  scrmDeliveryPlans.stream().collect(Collectors.toMap(ScrmDeliveryPlan::getId,ScrmDeliveryPlan::getPlanName));
            list.forEach(item->{
                item.setPlanName( planNameMap.get(item.getPlanId()));
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出企业微信客户详情信息列表
     */
    @PreAuthorize("@ss.hasPermi('scrm:info:export')")
    @Log(title = "企业微信客户详情信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrmCustomerInfo scrmCustomerInfo)
    {
        List<ScrmCustomerInfo> list = scrmCustomerInfoService.selectScrmCustomerInfoList(scrmCustomerInfo);
        ExcelUtil<ScrmCustomerInfo> util = new ExcelUtil<ScrmCustomerInfo>(ScrmCustomerInfo.class);
        util.exportExcel(response, list, "企业微信客户详情信息数据");
    }

    /**
     * 获取企业微信客户详情信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(scrmCustomerInfoService.selectScrmCustomerInfoById(id));
    }

    /**
     * 新增企业微信客户详情信息
     */
    @PreAuthorize("@ss.hasPermi('scrm:info:add')")
    @Log(title = "企业微信客户详情信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScrmCustomerInfo scrmCustomerInfo)
    {
        return toAjax(scrmCustomerInfoService.insertScrmCustomerInfo(scrmCustomerInfo));
    }

    /**
     * 修改企业微信客户详情信息
     */
    @PreAuthorize("@ss.hasPermi('scrm:info:edit') || @ss.hasPermi('advanced:info:edit')")
    @Log(title = "企业微信客户详情信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScrmCustomerInfo scrmCustomerInfo)
    {
        return toAjax(scrmCustomerInfoService.updateScrmCustomerInfo(scrmCustomerInfo));
    }

    /**
     * 删除企业微信客户详情信息
     */
    @PreAuthorize("@ss.hasPermi('scrm:info:remove')")
    @Log(title = "企业微信客户详情信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(scrmCustomerInfoService.deleteScrmCustomerInfoByIds(ids));
    }

    /**
     * 批量修改跟进状态
     */
    @PreAuthorize("@ss.hasPermi('scrm:info:status')")
    @Log(title = "批量修改跟进状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updatefoll")
    public AjaxResult editFoll(@RequestBody BatchParam param)
    {
        List<ScrmCustomerInfo> updateList =  new ArrayList<>();
        param.getIds().forEach(id->{
            ScrmCustomerInfo item = new ScrmCustomerInfo();
            item.setId(id);
            item.setFollowStatus(param.getFollowStatus());
            updateList.add(item);
        });
        return toAjax(scrmCustomerInfoService.updateBatchById(updateList));
    }

    // 包装对象
    @Data
    public static class BatchParam {
        private List<Integer> ids;
        private String followStatus;
    }

}
